// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.chat.defaultChats;

import java.nio.ByteBuffer;
import java.util.HashMap;
import zombie.characters.IsoPlayer;
import zombie.chat.ChatBase;
import zombie.chat.ChatElement;
import zombie.chat.ChatMessage;
import zombie.chat.ChatMode;
import zombie.chat.ChatTab;
import zombie.chat.ChatUtility;
import zombie.core.Color;
import zombie.core.fonts.AngelCodeFont;
import zombie.debug.DebugLog;
import zombie.network.GameClient;
import zombie.network.chat.ChatType;
import zombie.ui.TextManager;
import zombie.ui.UIFont;

public abstract class RangeBasedChat extends ChatBase {
    private static ChatElement overHeadChat = null;
    private static HashMap<String, IsoPlayer> players = null;
    private static String currentPlayerName = null;
    String customTag = "default";

    RangeBasedChat(ByteBuffer byteBuffer, ChatType chatType, ChatTab chatTab, IsoPlayer isoPlayer) {
        super(byteBuffer, chatType, chatTab, isoPlayer);
    }

    RangeBasedChat(ChatType chatType) {
        super(chatType);
    }

    RangeBasedChat(int _int, ChatType chatType, ChatTab chatTab) {
        super(_int, chatType, chatTab);
    }

    public void Init() {
        currentPlayerName = this.getChatOwnerName();
        if (players != null) {
            players.clear();
        }

        overHeadChat = this.getChatOwner().getChatElement();
    }

    @Override
    public boolean isSendingToRadio() {
        return true;
    }

    @Override
    public ChatMessage createMessage(String text) {
        ChatMessage chatMessage = super.createMessage(text);
        if (this.getMode() == ChatMode.SinglePlayer) {
            chatMessage.setShowInChat(false);
        }

        chatMessage.setOverHeadSpeech(true);
        chatMessage.setShouldAttractZombies(true);
        return chatMessage;
    }

    public ChatMessage createBubbleMessage(String text) {
        ChatMessage chatMessage = super.createMessage(text);
        chatMessage.setOverHeadSpeech(true);
        chatMessage.setShowInChat(false);
        return chatMessage;
    }

    @Override
    public void sendMessageToChatMembers(ChatMessage msg) {
        IsoPlayer isoPlayer0 = ChatUtility.findPlayer(msg.getAuthor());
        if (this.getRange() == -1.0F) {
            DebugLog.log("Range not set for '" + this.getTitle() + "' chat. Message '" + msg.getText() + "' ignored");
        } else {
            for (short _short : this.members) {
                IsoPlayer isoPlayer1 = ChatUtility.findPlayer(_short);
                if (isoPlayer1 != null && isoPlayer0.getOnlineID() != _short && ChatUtility.getDistance(isoPlayer0, isoPlayer1) < this.getRange()) {
                    this.sendMessageToPlayer(_short, msg);
                }
            }
        }
    }

    @Override
    public void showMessage(ChatMessage msg) {
        super.showMessage(msg);
        if (msg.isOverHeadSpeech()) {
            this.showInSpeechBubble(msg);
        }
    }

    protected ChatElement getSpeechBubble() {
        return overHeadChat;
    }

    protected UIFont selectFont(String string) {
        char[] _char = string.toCharArray();
        UIFont uIFont = UIFont.Dialogue;
        AngelCodeFont angelCodeFont = TextManager.instance.getFontFromEnum(uIFont);

        for (int _int = 0; _int < _char.length; _int++) {
            if (_char[_int] > angelCodeFont.chars.length) {
                uIFont = UIFont.Medium;
                break;
            }
        }

        return uIFont;
    }

    protected void showInSpeechBubble(ChatMessage chatMessage) {
        Color color = this.getColor();
        String string0 = chatMessage.getAuthor();
        IsoPlayer isoPlayer0 = this.getPlayer(string0);
        float float0 = color.r;
        float float1 = color.g;
        float float2 = color.b;
        if (isoPlayer0 != null) {
            float0 = isoPlayer0.getSpeakColour().r;
            float1 = isoPlayer0.getSpeakColour().g;
            float2 = isoPlayer0.getSpeakColour().b;
        }

        String string1 = ChatUtility.parseStringForChatBubble(chatMessage.getText());
        if (string0 != null && !"".equalsIgnoreCase(string0) && !string0.equalsIgnoreCase(currentPlayerName)) {
            if (!players.containsKey(string0)) {
                players.put(string0, this.getPlayer(string0));
            }

            IsoPlayer isoPlayer1 = (IsoPlayer)players.get(string0);
            if (isoPlayer1 != null) {
                if (isoPlayer1.isDead()) {
                    isoPlayer1 = this.getPlayer(string0);
                    players.replace(string0, isoPlayer1);
                }

                isoPlayer1.getChatElement()
                    .addChatLine(
                        string1,
                        float0,
                        float1,
                        float2,
                        this.selectFont(string1),
                        this.getRange(),
                        this.customTag,
                        this.isAllowBBcode(),
                        this.isAllowImages(),
                        this.isAllowChatIcons(),
                        this.isAllowColors(),
                        this.isAllowFonts(),
                        this.isEqualizeLineHeights()
                    );
            }
        } else {
            overHeadChat.addChatLine(
                string1,
                float0,
                float1,
                float2,
                this.selectFont(string1),
                this.getRange(),
                this.customTag,
                this.isAllowBBcode(),
                this.isAllowImages(),
                this.isAllowChatIcons(),
                this.isAllowColors(),
                this.isAllowFonts(),
                this.isEqualizeLineHeights()
            );
        }
    }

    private IsoPlayer getPlayer(String string) {
        IsoPlayer isoPlayer = GameClient.bClient ? GameClient.instance.getPlayerFromUsername(string) : null;
        if (isoPlayer != null) {
            return isoPlayer;
        } else {
            for (int _int = 0; _int < IsoPlayer.numPlayers; _int++) {
                isoPlayer = IsoPlayer.players[_int];
                if (isoPlayer != null && isoPlayer.getUsername().equals(string)) {
                    return isoPlayer;
                }
            }

            return null;
        }
    }

    static {
        players = new HashMap();
    }
}
